
enum LINE8_CHANNELS {
    //% block="通道1"
    C0,
    //% block="通道2"
    C1,
    //% block="通道3"
    C2,
    //% block="通道4"
    C3,
    //% block="通道5"
    C4,
    //% block="通道6"
    C5,
    //% block="通道7"
    C6,
    //% block="通道8"
    C7,
}

enum LINE5_CHANNELS {
    //% block="通道0"
    C0,
    //% block="通道1"
    C1,
    //% block="通道2"
    C2,
    //% block="通道3"
    C3,
    //% block="通道4"
    C4,
}


//% color="#ee7959" iconWidth=50 iconHeight=40
namespace MicroblueFollower{
    
    //% block="8路巡线串口模式，初始化: TX=[TX], RX=[RX]" blockType="command"
    //% RX.shadow="dropdownRound" RX.options="PINS" RX.defl=PINS.P19
    //% TX.shadow="dropdownRound" TX.options="PINS" TX.defl="PINS.P20"
    export function line8_ttl_init(parameter: any, block: any) {
        let rx = parameter.RX.code; 
        let tx = parameter.TX.code;
        Generator.addImport("from mb_follower import FollowerLine8Ttl", true);
        Generator.addInitHeader("line8", `line8 = FollowerLine8Ttl(rx=${rx}, tx=${tx})`, true);
    }


    //% block="8路巡线串口模式: 读取数据" blockType="reporter"
    export function line8_ttl_read_data(parameter: any, block: any) {
        Generator.addCode(`line8.read_data()`);
    }

    //% block="8路巡线串口模式: 读取[C]的数据" blockType="reporter"
    //% C.shadow="dropdownRound"   C.options="LINE8_CHANNELS"     C.defl=LINE8_CHANNELS.C0
    export function line8_ttl_read_data_by_index(parameter: any, block: any) {
        let c = parameter.C.code;
        Generator.addCode(`line8.read_data()[${c[1]}]`);
    }


    //% block="8路巡线I2C模式，初始化: SDA=[SDA], SCL=[SCL]" blockType="command"
    //% SCL.shadow="dropdownRound" SCL.options="PINS" SCL.defl=PINS.P19
    //% SDA.shadow="dropdownRound" SDA.options="PINS" SDA.defl=PINS.P20
    export function line8_i2c_init(parameter: any, block: any) {
        let sda = parameter.SDA.code; 
        let scl = parameter.SCL.code;
        Generator.addImport("from mb_follower import FollowerLine8I2c", true);
        Generator.addInitHeader("line8", `line8 = FollowerLine8I2c(sda=${sda}, scl=${scl})`, true);
    }


    //% block="8路巡线I2C模式: 读取模拟数据" blockType="reporter"
    export function line8_i2c_read_analog_data(parameter: any, block: any) {
        Generator.addCode(`line8.analog_values()`);
    }

    //% block="8路巡线I2C模式: 读取[C]的模拟数据" blockType="reporter"
    //% C.shadow="dropdownRound"   C.options="LINE8_CHANNELS"     C.defl=LINE8_CHANNELS.C0
    export function line8_i2c_read_analog_data_by_index(parameter: any, block: any) {
        let c = parameter.C.code;
        Generator.addCode(`line8.analog_values()[${c[1]}]`);
    }
    

    //% block="8路巡线I2C模式: 读取数字数据" blockType="reporter"
    export function line8_i2c_read_digital_data(parameter: any, block: any) {
        Generator.addCode(`line8.digital_values()`);
    }

    //% block="8路巡线I2C模式: 读取[C]的数字数据" blockType="reporter"
    //% C.shadow="dropdownRound"   C.options="LINE8_CHANNELS"     C.defl=LINE8_CHANNELS.C0
    export function line8_i2c_read_digital_data_by_index(parameter: any, block: any) {
        let c = parameter.C.code;
        Generator.addCode(`line8.digital_values()[${c[1]}]`);
    }


    //% block="8路巡线(V2)I2C模式，初始化: SDA=[SDA], SCL=[SCL]" blockType="command"
    //% SCL.shadow="dropdownRound" SCL.options="PINS" SCL.defl=PINS.P19
    //% SDA.shadow="dropdownRound" SDA.options="PINS" SDA.defl=PINS.P20
    export function line8V2_i2c_init(parameter: any, block: any) {
        let sda = parameter.SDA.code; 
        let scl = parameter.SCL.code;
        Generator.addImport("from mb_follower import FollowerLine8I2cYb", true);
        Generator.addInitHeader("line8", `line8 = FollowerLine8I2cYb(sda=${sda}, scl=${scl})`, true);
    }

    //% block="8路巡线(V2)I2C模式: 读取数字数据" blockType="reporter"
    export function line8V2_i2c_read_digital_data(parameter: any, block: any) {
        Generator.addCode(`line8.digital_values()`);
    }

    //% block="8路巡线(V2)I2C模式: 读取[C]的数字数据" blockType="reporter"
    //% C.shadow="dropdownRound"   C.options="LINE8_CHANNELS"     C.defl=LINE8_CHANNELS.C0
    export function line8V2_i2c_read_digital_data_by_index(parameter: any, block: any) {
        let c = parameter.C.code;
        Generator.addCode(`line8.digital_values()[${c[1]}]`);
    }

    //% block="5路灰度巡线，初始化: SDA=[SDA], SCL=[SCL]" blockType="command"
    //% SCL.shadow="dropdownRound" SCL.options="PINS" SCL.defl=PINS.P19
    //% SDA.shadow="dropdownRound" SDA.options="PINS" SDA.defl=PINS.P20
    export function line5_gray_init(parameter: any, block: any) {
        let sda = parameter.SDA.code; 
        let scl = parameter.SCL.code;
        Generator.addImport("from mb_follower import FollowerLine5Gray", true);
        Generator.addInitHeader("line5", `line5 = FollowerLine5Gray(sda=${sda}, scl=${scl})`, true);
    }


    //% block="5路灰度巡线，设置低阈值: 通道0=[X0], 通道1=[X1], 通道2=[X2], 通道3=[X3], 通道4=[X4]" blockType="command"
    //% X0.shadow="number" X0.min=0 X0.max=1000  X0.defl=500
    //% X1.shadow="number" X1.min=0 X1.max=1000  X1.defl=500
    //% X2.shadow="number" X0.min=0 X2.max=1000  X2.defl=500
    //% X3.shadow="number" X0.min=0 X3.max=1000  X3.defl=500
    //% X4.shadow="number" X0.min=0 X4.max=1000  X4.defl=500
    export function line5_gray_set_low_thresholds(parameter: any, block: any) {
        let x0 = parameter.X0.code;
        let x1 = parameter.X1.code;
        let x2 = parameter.X2.code;
        let x3 = parameter.X3.code;
        let x4 = parameter.X4.code;
        Generator.addCode(`line5.low_thresholds = (${x0}, ${x1}, ${x2}, ${x3}, ${x4})`);
    }


    //% block="5路灰度巡线，设置高阈值: 通道0=[X0], 通道1=[X1], 通道2=[X2], 通道3=[X3], 通道4=[X4]" blockType="command"
    //% X0.shadow="number" X0.min=0 X0.max=1000  X0.defl=800
    //% X1.shadow="number" X1.min=0 X1.max=1000  X1.defl=800
    //% X2.shadow="number" X0.min=0 X2.max=1000  X2.defl=800
    //% X3.shadow="number" X0.min=0 X3.max=1000  X3.defl=800
    //% X4.shadow="number" X0.min=0 X4.max=1000  X4.defl=800
    export function line5_gray_set_high_thresholds(parameter: any, block: any) {
        let x0 = parameter.X0.code;
        let x1 = parameter.X1.code;
        let x2 = parameter.X2.code;
        let x3 = parameter.X3.code;
        let x4 = parameter.X4.code;
        Generator.addCode(`line5.high_thresholds = (${x0}, ${x1}, ${x2}, ${x3}, ${x4})`);
    }

    //% block="5路灰度巡线: 读取模拟数据" blockType="reporter"
    export function line5_gray_read_analog_data(parameter: any, block: any) {
        Generator.addCode(`line5.analog_values`);
    }

    //% block="5路灰度巡线: 读取[C]的模拟数据" blockType="reporter"
    //% C.shadow="dropdownRound"   C.options="LINE5_CHANNELS"     C.defl=LINE5_CHANNELS.C0
    export function line5_gray_read_analog_data_by_index(parameter: any, block: any) {
        let c = parameter.C.code;
        Generator.addCode(`line5.analog_values[${c[1]}]`);
    }
    

    //% block="5路灰度巡线: 读取数字数据" blockType="reporter"
    export function line5_gray_read_digital_data(parameter: any, block: any) {
        Generator.addCode(`line5.digital_values`);
    }

    //% block="5路灰度巡线: 读取[C]的数字数据" blockType="reporter"
    //% C.shadow="dropdownRound"   C.options="LINE5_CHANNELS"     C.defl=LINE5_CHANNELS.C0
    export function line5_gray_read_digital_data_by_index(parameter: any, block: any) {
        let c = parameter.C.code;
        Generator.addCode(`line5.digital_values[${c[1]}]`);
    }

    
    //% block="当巡线传感器每隔[PERIOD]毫秒有数据返回时" blockType="hat"
    //% PERIOD.shadow="number"    PERIOD.defl="20"
    export function control_receive(parameter: any, block: any) {
        let period = parameter.PERIOD.code;
        Generator.addImport("from mb_follower import FollowerTimer", true);
        Generator.addInitHeader("follower_timer_init", `follower_tim = FollowerTimer()`, true)
        Generator.addInitHeader("follower_tim_callback", `follower_tim.connect(follower_tim_callback, ${period})`, true)
        Generator.addEvent("FollowerTimCallback","follower_tim_callback", '');
    }
}
